/**
 * Project Name:swt-hello <br/>
 * File Name:App.java <br/>
 * Package Name:com.ftu.z <br/>
 */
package com.ftu.swt.basic4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


/**
 * <p>  </p>
 * @version 1.0 2018年11月22日 mq 修改内容:初版
 */
public class UsingtList {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        Shell shell1 = new Shell(display);
        shell1.setText("shell1");
        shell1.setSize(400, 400);
        
        final List list = new List(shell1, SWT.BORDER);
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.setBounds(10, 10, 115, 150);
        System.out.println(list.getItemCount());
        
        list.addSelectionListener(new SelectionListener() {
            private int oldIdx = -1;
            private String oldText = null;
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(oldIdx != -1) {
                    list.setItem(oldIdx, oldText);
                }
                int idx = list.getSelectionIndex();
                oldIdx = idx;
                oldText = list.getItem(idx);
                list.setItem(idx, oldText + " selected");
            }
            
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                if(oldIdx != -1) {
                    list.setItem(oldIdx, oldText);
                }
                int idx = list.getSelectionIndex();
                oldIdx = idx;
                oldText = list.getItem(idx);
                list.setItem(idx, oldText + " default selected");
            }
        });
        
        
        
        
        
        
        
        shell1.open();
        while(!shell1.isDisposed()) {
            if(!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
        
    }

}
